package com.ideal.manage.esms.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;

public class DateBtnUtil {
    /**
     * 今天
     *
     * @return
     * @throws ParseException
     */

    public static List<Date> today() throws ParseException {
        List<Date> listDate = new ArrayList<>();
        //今天
        Date date = new Date();
        //明天
        Date date2 = new Date(date.getTime() + 24 * 3600 * 1000);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = dateFormat.format(date);
        String formatDate2 = dateFormat.format(date2);

        Date today = dateFormat.parse(formatDate);
        Date tomorrow = dateFormat.parse(formatDate2);

        listDate.add(today);
        listDate.add(tomorrow);
        return listDate;
    }

    /**
     * 昨天
     *
     * @return
     * @throws ParseException
     */
    public static List<Date> yestoday() throws ParseException {
        List<Date> listDate = new ArrayList<>();
        //今天
        Date date = new Date();
        //昨天   86399000
        long l = date.getTime() - 24L * 3600L * 1000L;
        Date date2 = new Date(l);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       /* String formatDate = df.format(dateFormat.parse(dateFormat.format(l)).getTime() + 86399000L);*/
        String formatDate2 = dateFormat.format(date2);

        Date today = dateFormat.parse(formatDate2);
        Date yestoday = df.parse(df.format(new Date(today.getTime() + 86399000L)));

        listDate.add(today);
        listDate.add(yestoday);
        return listDate;
    }

    /**
     * 本周
     *
     * @return
     * @throws ParseException
     */
    public static List<Date> week() throws ParseException {
        List<Date> listDate = new ArrayList<>();
        //获取周一的值
        LocalDate today = LocalDate.now();
        LocalDate toweekMonday = today.with(DayOfWeek.MONDAY);
        LocalDate sunDay = today.with(DayOfWeek.SUNDAY);

        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = toweekMonday.atStartOfDay(zoneId);
        Date monday = Date.from(zonedDateTime.toInstant());
        ZonedDateTime zonedDateTime2 = sunDay.atStartOfDay(zoneId);
        Date sunday = Date.from(zonedDateTime2.toInstant());            //转化date类型
        listDate.add(monday);
        listDate.add(sunday);
        return listDate;
    }

    /**
     * 本月
     *
     * @return
     * @throws ParseException
     */
    public static List<Date> month() throws ParseException {
        List<Date> listDate = new ArrayList<>();
        /**
         * 本月第一天
         */
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        GregorianCalendar gc2 = (GregorianCalendar) Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        gc2.setTime(new Date());
        gc2.set(Calendar.DAY_OF_MONTH, 1);                  //设置该月的第一天
        String b_day_first = df.format(gc2.getTime());
        StringBuffer str_b = new StringBuffer().append(b_day_first).append(" 00:00:00");//拼接 时分秒
        b_day_first = str_b.toString();

        /**
         * 本月最后一天
         */
        calendar2.add(Calendar.MONTH, 1);        //加一个月
        calendar2.set(Calendar.DATE, 1);                 //设置为该月第一天
        calendar2.add(Calendar.DATE, -1);       //再减一天即为上个月最后一天
        String b_day_last = df.format(calendar2.getTime());
        StringBuffer endStrb = new StringBuffer().append(b_day_last).append(" 23:59:59");   //拼接 时分秒
        b_day_last = endStrb.toString();

        Date monthFirst = df.parse(b_day_first);
        Date monthLast = df.parse(b_day_last);

        listDate.add(monthFirst);
        listDate.add(monthLast);
        return listDate;
    }


    //获取本年的开始时间
    public static java.util.Date getBeginDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getNowYear());
        // cal.set
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DATE, 1);

        return getDayStartTime(cal.getTime());
    }

    //获取本年的结束时间
    public static java.util.Date getEndDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getNowYear());
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DATE, 31);
        return getDayEndTime(cal.getTime());
    }

    //获取某个日期的开始时间
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {calendar.setTime(d);}
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    //获取某个日期的结束时间
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }

    //获取今年是哪一年
    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }

    public static Date dataFormat(Date date) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = dateFormat.parse(String.valueOf(date));
        return parse;
    }


    /**
     * 获得某月第一天
     * @param month
     * @return
     */
    public static Date getFirstDayOfMonth(int month) throws ParseException {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, getNowYear());
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        //获取某月最小天数
        //设置日历中月份的最小天数
        cal.set(Calendar.DATE, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDayOfMonth = sdf.format(cal.getTime());
        StringBuffer endStrb = new StringBuffer().append(firstDayOfMonth).append(" 00:00:00");
        String b_day_last = endStrb.toString();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date monthEnd = df.parse(b_day_last);
        return monthEnd;
    }

    /**
     * 获得某月最后一天
     * @param month
     * @return
     */
    public static Date getLastDayOfMonth(int month) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        // 设置时间,当前时间不用设置
        calendar.set(Calendar.YEAR, getNowYear());
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd ");
        String format1 = format.format(calendar.getTime());

        StringBuffer endStrb = new StringBuffer().append(format1).append(" 23:59:59");
        String b_day_last = endStrb.toString();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date monthEnd = df.parse(b_day_last);

        return monthEnd;

    }


    /**
     * 根据指定月，返回所有天
     * @param year
     * @param month  指定月
     * @return
     * @throws ParseException
     */
    public static List<String> getMonthAllDays(int year,int month) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year,month,1);
        int dayInt = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        List<String> days = new ArrayList<>();
        for(int i =1; i<= dayInt; i++){
            if(i < 10){
                days.add(year+"-"+month+"-0"+i);
            }else{
                days.add(year+"-"+month+"-"+i);
            }
        }
        return  days;
    }

}
